Flutter 端:
lib/batterylevel.dart
:新增取得電池電量資訊方法,透過建立的MethodChannel
傳入方法名來呼叫Native 端對應的getBatteryLevel
方法
static Future<String?> get batteryLevel async {
final String? batteryLevel = await _channel.invokeMethod('getBatteryLevel');
return batteryLevel;
}
Native 端(Android):
android/.../BatterylevelPlugin.kt
:
這邊會取得電量會需要android 的applicationContext
,所以我們新增一個applicationContext
屬性,在onAttachedToEngine
時透過flutterPluginBinding
取得,並在onDetachedFromEngine
釋放
最後新增一個方法getBatteryLevel
來處理取得電量資訊的動作
最後在onMethodCall
的地方實現從Flutter 端要調用的getBatteryLevel
方法
class BatterylevelPlugin : FlutterPlugin, MethodCallHandler {
private lateinit var channel: MethodChannel
private var applicationContext: Context? = null
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
applicationContext = flutterPluginBinding.applicationContext
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "batterylevel")
channel.setMethodCallHandler(this)
}
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method == "getPlatformVersion") {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
} else if (call.method == "getBatteryLevel") {
val batterLevel = getBatteryLevel()
if (batterLevel != -1) {
result.success("Android batterLevel = $batterLevel%")
} else {
result.error("UNAVAILABLE", "Battery level not available.", null);
}
} else {
result.notImplemented()
}
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
applicationContext = null;
channel.setMethodCallHandler(null)
}
private fun getBatteryLevel(): Int {
var batteryLevel = -1
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val batteryManager =
applicationContext?.let { getSystemService(it, BatteryManager::class.java) }
batteryLevel = batteryManager!!.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
} else {
ContextWrapper(applicationContext).registerReceiver(
null,
IntentFilter(Intent.ACTION_BATTERY_CHANGED)
)?.apply {
batteryLevel = getIntExtra(
BatteryManager.EXTRA_LEVEL,
-1
) * 100 / getIntExtra(BatteryManager.EXTRA_SCALE, -1)
}
}
return batteryLevel
}
}
Native 端(iOS):
同樣新增一個方法getBatteryLevel
來處理取得電量資訊的動作,最後在handle
實現從Flutter 端要調用的getBatteryLevel
方法
public class SwiftBatterylevelPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "batterylevel", binaryMessenger: registrar.messenger())
let instance = SwiftBatterylevelPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "getPlatformVersion":
result("iOS " + UIDevice.current.systemVersion)
case "getBatteryLevel":
let batterLevel = getBatteryLevel()
if(batterLevel == -1) {
result(FlutterError(code: "UNAVAILABLE", message: "Battery info unavailable", details: nil));
} else {
result("iOS batterLevel = \(batterLevel * 100)%")
}
default:
result(FlutterMethodNotImplemented)
}
}
private func getBatteryLevel() -> Float {
let device = UIDevice.current
device.isBatteryMonitoringEnabled = true
if device.batteryState == .unknown {
return -1;
} else {
return UIDevice.current.batteryLevel
}
}
}
這樣就完成獲得設備電池電量資訊的插件功能了,並了解對MethodChannel
的實作步驟流程,接下來讓我們看看其他的Channel
要如何實作吧